package org.sauceggplant.item.man.ui.panel;

import org.sauceggplant.item.man.ui.toolbar.ToolBar;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class CardPanel extends JPanel {

    private Component[][] rowData;

    private ToolBar toolBar;

    private JLabel queryParam;

    public CardPanel(Component[][] rowData, JLabel queryParam) {
        this.rowData = rowData;
        this.queryParam = queryParam;
        init();
    }

    private void init() {
        this.setLayout(new BorderLayout());
        initComponent();
    }

    private void initComponent() {
        toolBar = new ToolBar(queryParam);
        this.add(toolBar, BorderLayout.NORTH);
        JPanel componentPanel = new JPanel();
        componentPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        for (int i = 0; i < rowData.length; i++) {
            for (int j = 0; j < rowData[0].length; j++) {
                componentPanel.add(rowData[i][j]);
            }
        }
        componentPanel.setBorder(new TitledBorder(new EmptyBorder(5, 5, 5, 5), ""));
        this.add(componentPanel, BorderLayout.CENTER);
    }

    public JTextField getQueryParamValue(){
        return toolBar.getQueryParamValue();
    }

    public JButton getQuery() {
        return toolBar.getQuery();
    }

    public JButton getAdd() {
        return toolBar.getAdd();
    }

    public JButton getSave() {
        return toolBar.getSave();
    }

    public JButton getDelete() {
        return toolBar.getDelete();
    }

    public static String getIndex(int index) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(fillString("0", String.valueOf(index).length()));
        stringBuffer.append(index);
        return stringBuffer.toString();
    }

    public static String fillString(String s, int length) {
        StringBuffer stringBuffer = new StringBuffer();
        int len = 4 - length;
        for (int i = 0; i < len; i++) {
            stringBuffer.append(s);
        }
        return stringBuffer.toString();
    }
}
